<?php
namespace Corporate\Controller;

use Zend\View\Model\ModelInterface;
use Zend\Form\Form;
use Predis\Client;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use SanAuthWithDbSaveHandler\Storage\IdentityManagerInterface;
use Zend\View\Model\JsonModel;
use ZfcUser\Model\TuserTable;
use ZfcUser\Model\Tuser;
use ZfcUser\Model\Torders;
use ZfcUser\Model\TordersTable;
use Zend\Cache\StorageFactory;
use Zend\Session\Container;
use Users\Model\Rates;
use Users\Model\RatesTable;

class CorporateController extends AbstractActionController
{	
	protected $ratesTable;
	protected $navigation;
	
	public function headerAction($params)
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$baseUrl = $baseUrlArr['baseUrl'];
		$basePath = $baseUrlArr['basePath'];
		$uploadPath = $baseUrlArr['uploadPath'];
		return $this->layout()->setVariable(
			"headerarray",array(
				'baseUrl' 		=> 	$baseUrl,
				'basePath'		=>	$basePath,
				'uploadPath'	=>	$uploadPath,
			)
		);
	}
	public function logoutAction()
    {
	
	}
	public function cdashboardAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			//get link man I.E NAME START
			$url2='http://176.9.51.8:8080/external/server/GetCustomer';
			$data2='{"accounts":["'.$SESSION['uname'].'"]}';
			$chlead2 = curl_init();
			curl_setopt($chlead2, CURLOPT_URL, $url2);
			curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data2)));
			curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead2, CURLOPT_POSTFIELDS,$data2);
			curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
			$account2 = curl_exec($chlead2);
			$addresserror2 = curl_error($chlead2);
			$data2=json_decode($account2,true);
			$SESSION = new Container('user');
			$SESSION['cname']=$data2['infoCustomers'][0]['infoCustomerAdditional']['linkMan'];
			//current Pakages
			$url3='http://176.9.51.8:8080/external/server/GetSuiteOrder';
			$data3='{"ownerName":"'.$SESSION['uname'].'","ownerType":2}';
			$chlead3 = curl_init();
			curl_setopt($chlead3, CURLOPT_URL, $url3);
			curl_setopt($chlead3, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead3, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data3)));
			curl_setopt($chlead3, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead3, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead3, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead3, CURLOPT_POSTFIELDS,$data3);
			curl_setopt($chlead3, CURLOPT_SSL_VERIFYPEER, 0);
			$Suite = curl_exec($chlead3);
			$addresserror3 = curl_error($chlead3);
			$data3=json_decode($Suite,true);
			//echo '<pre>';print_r($data2);exit;
			$a=array();
			$b=array();
			$c=array();
			if(count($data3['infoSuiteOrders'])!='' && count($data3['infoSuiteOrders'])!='0'){
				foreach($data3['infoSuiteOrders'] as $data){ 
					array_push($a,$data['suiteId']);
				}
			}
			for($i=0;$i<count($a);$i++){
				$url4='http://176.9.51.8:8080/external/server/GetSuite';
				$data4='{"ids":["'.$a[$i].'"]}';
				$chlead4 = curl_init();
				curl_setopt($chlead4, CURLOPT_URL, $url4);
				curl_setopt($chlead4, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead4, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data4)));
				curl_setopt($chlead4, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead4, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead4, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead4, CURLOPT_POSTFIELDS,$data4);
				curl_setopt($chlead4, CURLOPT_SSL_VERIFYPEER, 0);
				$Suitename = curl_exec($chlead4);
				$addresserror4 = curl_error($chlead4);
				$data4=json_decode($Suitename,true);
				$SESSION = new Container('user');
				
				array_push($b,$data4['infoSuites'][0]['name']);
				if(isset($data4['infoSuites'][0]['infoGiftTimes'][0]['giftTime'])){
					array_push($c,$data4['infoSuites'][0]['infoGiftTimes'][0]['giftTime']);
				}else{
					array_push($c," ");
				}
			}
			//1day difference
			/*$date_raw=date("Y-m-d");
			$first_date = strtotime($date_raw);
			$currentDate=date('Ymd', $first_date);
			$second_date = strtotime('-1 day', $first_date);
			$lastData=date('Ymd', $second_date);
			
			//Latest calls and Graph 
			$url5='http://176.9.51.8:8080/external/server/GetCdr';
			$data5='{"accounts":["'.$SESSION['uname'].'"],"beginTime":"'.$lastData.'","endTime":"'.$currentDate.'"}';
			$chlead5 = curl_init();
			curl_setopt($chlead5, CURLOPT_URL, $url5);
			curl_setopt($chlead5, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead5, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data5)));
			curl_setopt($chlead5, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead5, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead5, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead5, CURLOPT_POSTFIELDS,$data5);
			curl_setopt($chlead5, CURLOPT_SSL_VERIFYPEER, 0);
			$Graph = curl_exec($chlead5);
			$addresserror = curl_error($chlead5);
			$Cgraph=json_decode($Graph,true);
			$count=count($Cgraph['infoCdrs']);
			$SESSION = new Container('user');
			if($count==0){
				$SESSION['graphC']=0;
			}else{
				$SESSION['Cgraph']='';
				$SESSION['graphC']=1;
			}*/
			$result= new ViewModel(array(
				"data" 			 =>	$data2,
				"Suitename"		 =>	$b,
				"giftTime"		 =>	$c,
				//"Cgraph"		 =>	$Cgraph,
				
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}  
    }
	public function caccountAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			$success=0;$update=0;
			if($_POST){
				$update=1;
					$url='http://176.9.51.8:8080/external/server/ModifyCustomer';
					$data='{"account":"'.$SESSION['uname'].'","infoCustomerAdditional":{"cardType":'.$_POST['CorpId'].',"cardNumber":"'.$_POST['datafile'].'","linkMan":"'.$_POST['uname'].'","email":"'.$_POST['Email'].'"}}';
					$chlead = curl_init();
					curl_setopt($chlead, CURLOPT_URL, $url);
					curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
					curl_setopt($chlead, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
					curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
					$account = curl_exec($chlead);
					$addresserror = curl_error($chlead);
					$dat=json_decode($account,true);
					//password modify
					$url5='http://176.9.51.8:8080/external/server/ModifyGatewayMapping';
					$data5='{"account":"'.$SESSION['uname'].'","name":"'.$SESSION['uname'].'","password":"'.$_POST['pass'].'","customerPassword":"'.$_POST['pass'].'"}';
					$chlead5 = curl_init();
					curl_setopt($chlead5, CURLOPT_URL, $url5);
					curl_setopt($chlead5, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead5, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data5)));
					curl_setopt($chlead5, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead5, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead5, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead5, CURLOPT_POSTFIELDS,$data5);
					curl_setopt($chlead5, CURLOPT_SSL_VERIFYPEER, 0);
					$account5 = curl_exec($chlead5);
					$addresserror5 = curl_error($chlead5);
					$data5=json_decode($account5,true);
					if($dat['retCode']==0 && $data5['retCode']==0){
						$success=1;
					}
			}
			//current Pakages
			$url1='http://176.9.51.8:8080/external/server/GetSuiteOrder';
			$data1='{"ownerName":"'.$SESSION['uname'].'","ownerType":2}';
			$chlead1 = curl_init();
			curl_setopt($chlead1, CURLOPT_URL, $url1);
			curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
			curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
			curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
			$Suite = curl_exec($chlead1);
			$addresserror1 = curl_error($chlead1);
			$data1=json_decode($Suite,true);
			$a=array();
			$b=array();
			$c=array();
			if(count($data1['infoSuiteOrders'])!='' && count($data1['infoSuiteOrders'])!='0'){
				foreach($data1['infoSuiteOrders'] as $data){ 
					array_push($a,$data['suiteId']);
				}
			}
			for($i=0;$i<count($a);$i++){
				$url2='http://176.9.51.8:8080/external/server/GetSuite';
				$data2='{"ids":["'.$a[$i].'"]}';
				$chlead2 = curl_init();
				curl_setopt($chlead2, CURLOPT_URL, $url2);
				curl_setopt($chlead2, CURLOPT_USERAGENT, 'SugarConnector/1.4');
				curl_setopt($chlead2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data2)));
				curl_setopt($chlead2, CURLOPT_VERBOSE, 1);
				curl_setopt($chlead2, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($chlead2, CURLOPT_CUSTOMREQUEST, "POST"); 
				curl_setopt($chlead2, CURLOPT_POSTFIELDS,$data2);
				curl_setopt($chlead2, CURLOPT_SSL_VERIFYPEER, 0);
				$Suitename = curl_exec($chlead2);
				$addresserror2 = curl_error($chlead2);
				$data2=json_decode($Suitename,true);
				array_push($b,$data2['infoSuites'][0]['name']);
				if(isset($data2['infoSuites'][0]['infoGiftTimes'][0]['giftTime'])){
					array_push($c,$data2['infoSuites'][0]['infoGiftTimes'][0]['giftTime']);
				}else{
					array_push($c," ");
				}
			}
			//Get Customer Details
			$url3='http://176.9.51.8:8080/external/server/GetCustomer';
			$data3='{"accounts":["'.$SESSION['uname'].'"]}';
			$chlead3 = curl_init();
			curl_setopt($chlead3, CURLOPT_URL, $url3);
			curl_setopt($chlead3, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead3, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data3)));
			curl_setopt($chlead3, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead3, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead3, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead3, CURLOPT_POSTFIELDS,$data3);
			curl_setopt($chlead3, CURLOPT_SSL_VERIFYPEER, 0);
			$account3 = curl_exec($chlead3);
			$addresserror3 = curl_error($chlead3);
			$data3=json_decode($account3,true);
			$SESSION = new Container('user');
			$SESSION['cname']=$data3['infoCustomers'][0]['infoCustomerAdditional']['linkMan'];
			//get password
			$url4='http://176.9.51.8:8080/external/server/GetGatewayMapping';
			$data4='{"names":["'.$SESSION['uname'].'"]}';
			$chlead4 = curl_init();
			curl_setopt($chlead4, CURLOPT_URL, $url4);
			curl_setopt($chlead4, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead4, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data4)));
			curl_setopt($chlead4, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead4, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead4, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead4, CURLOPT_POSTFIELDS,$data4);
			curl_setopt($chlead4, CURLOPT_SSL_VERIFYPEER, 0);
			$account4 = curl_exec($chlead4);
			$addresserror4= curl_error($chlead4);
			$data4=json_decode($account4,true);
			$result= new ViewModel(array(
				"data1" 		 =>	$data4,
				"data" 			 =>	$data3,
				"Suitename"		 =>	$b,
				"giftTime"		 =>	$c,
				"success"    	 => $success,
				"update"		 =>	$update,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}		   
    }
	
	public function csvdata1Action()
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$uploadPath = $baseUrlArr['uploadPath'];
		$SESSION = new Container('user');
		$date_raw=date("Y-m-d");
		$first_date = strtotime($date_raw);
		$currentDate=date('Ymd', $first_date);
		$second_date = strtotime('-4 day', $first_date);
		$lastData=date('Ymd', $second_date);
		$csv_filename = $SESSION['uname']."_".date("Y-m-d",time()).".csv";
		
		$url5='http://176.9.51.8:8080/external/server/GetCdr';
		$data5='{"accounts":["'.$SESSION['uname'].'"],"beginTime":"'.$lastData.'","endTime":"'.$currentDate.'"}';
		$chlead5 = curl_init();
		curl_setopt($chlead5, CURLOPT_URL, $url5);
		curl_setopt($chlead5, CURLOPT_USERAGENT, 'SugarConnector/1.4');
		curl_setopt($chlead5, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data5)));
		curl_setopt($chlead5, CURLOPT_VERBOSE, 1);
		curl_setopt($chlead5, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($chlead5, CURLOPT_CUSTOMREQUEST, "POST"); 
		curl_setopt($chlead5, CURLOPT_POSTFIELDS,$data5);
		curl_setopt($chlead5, CURLOPT_SSL_VERIFYPEER, 0);
		$Graph = curl_exec($chlead5);
		$addresserror = curl_error($chlead5);
		$Cgraph=json_decode($Graph,true);
		if(isset($Cgraph['infoCdrs'])){
			$data=count($Cgraph['infoCdrs']);
			if($data>0){
				$list = array ();
				array_push($list,array('Number', 'Duration', 'Charge','Called Time','End Time'));
				foreach($Cgraph['infoCdrs'] as $data){
				
					$number=$data['calleeE164'];
					$original_timestamp=$data['start'];
					$timestamp = (int)substr($original_timestamp,0,-3); 
					$start=date('Y-m-d H:i:s',$timestamp);
					
					$original_timestamp1=$data['stop'];
					$timestamp1 = (int)substr($original_timestamp1,0,-3);
					$stop=date('Y-m-d H:i:s',$timestamp1);
				array_push($list,array('"'.$number.'"',gmdate("H:i:s", $data['feeTime']).' sec',$data['fee'],$start,$stop));
				}
				$path=$uploadPath.$csv_filename;
				$fp = fopen($path, 'w+ ');
				foreach ($list as $fields){
					fputcsv($fp, $fields);
				}
				fclose($fp);
			}else{
				$list = array ();
				array_push($list,array('No latest call history'));
				$path=$uploadPath.$csv_filename;
				$fp = fopen($path, 'w+ ');
				foreach ($list as $fields){
					fputcsv($fp, $fields);
				}
				fclose($fp);
			}
		}
		return $this->redirect()->toUrl('ccall-details');
	}
	public function csvdataAction()
    {
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$uploadPath = $baseUrlArr['uploadPath'];
		$SESSION = new Container('user');
		$date_raw=date("Y-m-d");
		$first_date = strtotime($date_raw);
		$currentDate=date('Ymd', $first_date);
		$second_date = strtotime('-1 day', $first_date);
		$lastData=date('Ymd', $second_date);
		$csv_filename = $SESSION['uname']."_".date("Y-m-d",time()).".csv";
		
		$url5='http://176.9.51.8:8080/external/server/GetCdr';
		$data5='{"accounts":["'.$SESSION['uname'].'"],"beginTime":"'.$lastData.'","endTime":"'.$currentDate.'"}';
		$chlead5 = curl_init();
		curl_setopt($chlead5, CURLOPT_URL, $url5);
		curl_setopt($chlead5, CURLOPT_USERAGENT, 'SugarConnector/1.4');
		curl_setopt($chlead5, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data5)));
		curl_setopt($chlead5, CURLOPT_VERBOSE, 1);
		curl_setopt($chlead5, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($chlead5, CURLOPT_CUSTOMREQUEST, "POST"); 
		curl_setopt($chlead5, CURLOPT_POSTFIELDS,$data5);
		curl_setopt($chlead5, CURLOPT_SSL_VERIFYPEER, 0);
		$Graph = curl_exec($chlead5);
		$addresserror = curl_error($chlead5);
		$Cgraph=json_decode($Graph,true);
		$list = array ();
		array_push($list,array('Number', 'Duration', 'Charge', 'Called Time','End Time'));
		foreach($Cgraph['infoCdrs'] as $data){
			array_push($list,array($data['calleeE164'],$data['feeTime'],$data['fee'],$data['start'],$data['stop']));
		}
		$path=$uploadPath.$csv_filename;
		$fp = fopen($path, 'w+ ');
		foreach ($list as $fields){
			fputcsv($fp, $fields);
		}
		fclose($fp);
		return $this->redirect()->toUrl('cusage-graph');
	}
	public function ccallDetailsAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			$baseUrls = $this->getServiceLocator()->get('config');
			$baseUrlArr = $baseUrls['urls'];
			$uploadPath = $baseUrlArr['uploadPath'];
			$result= new ViewModel(array(
				'uploadPath'	=>	$uploadPath,
			));
			return $result; 
		}else{
			return $this->redirect()->toUrl('login');
		}  
    }
	public function csettingsAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			if($_POST){
					$ipnum=$_POST['hidden'];
					$url='http://176.9.51.8:8080/external/server/ModifyGatewayMapping';
					$data='{"name":"'.$SESSION['uname'].'","account":"'.$SESSION['uname'].'","remoteIps":"'.$ipnum.'"}';
					$chlead = curl_init();
					curl_setopt($chlead, CURLOPT_URL, $url);
					curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
					curl_setopt($chlead, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
					curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
					$settings = curl_exec($chlead);
					$addresserror = curl_error($chlead);
					$data1=json_decode($settings);
					$retCode=$data1->retCode;
					if($retCode==0){
					return $this->redirect()->toUrl('csettings?id=3');	
					}else{
						return $this->redirect()->toUrl('csettings?id='.$retCode);	
					}	
			}
			$url1='http://176.9.51.8:8080/external/server/GetGatewayMapping';
			$data1='{"names":["'.$SESSION['uname'].'"]}';
			
			$chlead1 = curl_init();
			curl_setopt($chlead1, CURLOPT_URL, $url1);
			curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
			curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
			curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
			$account1 = curl_exec($chlead1);
			$addresserror1 = curl_error($chlead1);
			$data1=json_decode($account1,true);
			$result= new ViewModel(array(
				"data" 			 =>	$data1,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}   
    }
	public function cpaymentsAction()
    {	
		$SESSION = new Container('user');
		$baseUrls = $this->getServiceLocator()->get('config');
		$baseUrlArr = $baseUrls['urls'];
		$paypalUrl = $baseUrlArr['paypalUrl'];
		$paypalid = $baseUrlArr['paypalid'];
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			$url='http://176.9.51.8:8080/external/server/GetCustomer';
			$data='{"accounts":["'.$SESSION['uname'].'"]}';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$account = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$data=json_decode($account,true);
			$result= new ViewModel(array(
				"data" 			 =>	$data,
				"paypalUrl" 	 =>	$paypalUrl,
				"paypalid" 		 =>	$paypalid,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		} 	
    }
	public function cpaymentHistoryAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			//3 months differece
			$date_raw=date("Y-m-d");
			$first_date = strtotime($date_raw);
			$second_date = strtotime('+1 day', $first_date);
			$end_Date=date('Ymd', $second_date);
			$begin_Date = date("Ymd", strtotime("-3 months"));
			$t='000000';
			$end_Date=$end_Date.$t;
			$begin_Date=$begin_Date.$t;
			
			$url='http://176.9.51.8:8080/external/server/GetPayHistory';
			$data='{"account":"'.$SESSION['uname'].'","beginTime":"'.$begin_Date.'","endTime":"'.$end_Date.'"}';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$dataFinal=json_decode($dataTotal,true);
			$result= new ViewModel(array(
				"data" 			 =>	$dataFinal,
			));
			return $result;
		}else{
		return $this->redirect()->toUrl('login');
		}  
    }
	public function cusageGraphAction()
    {
       $SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			$baseUrls = $this->getServiceLocator()->get('config');
			$baseUrlArr = $baseUrls['urls'];
			$uploadPath = $baseUrlArr['uploadPath'];
			$result= new ViewModel(array(
				'uploadPath'	=>	$uploadPath,
				'data1'	=>	2,
			));
			return $result; 
		}else{
			return $this->redirect()->toUrl('login');
		}  
    }
	public function cpackages1Action()
    {
         $SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			//get money
			$url='http://176.9.51.8:8080/external/server/GetCustomer';
			$data='{"accounts":["'.$SESSION['uname'].'"]}';
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$data=json_decode($dataTotal,true);
			//get pakages
			$url1='http://176.9.51.8:8080/external/server/GetSuite';
			$data1='{}';
			$chlead1 = curl_init();
			curl_setopt($chlead1, CURLOPT_URL, $url1);
			curl_setopt($chlead1, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead1, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data1)));
			curl_setopt($chlead1, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead1, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead1, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead1, CURLOPT_POSTFIELDS,$data1);
			curl_setopt($chlead1, CURLOPT_SSL_VERIFYPEER, 0);
			$pakage = curl_exec($chlead1);
			$addresserror = curl_error($chlead1);
			$pakage1=json_decode($pakage,true);
			$result= new ViewModel(array(
				"data" 	=>	$data,
				"pakage" 	=>	$pakage1,
			));
			return $result;
		}else{
			return $this->redirect()->toUrl('login');
		}
    }
	public function cpackagesHistoryAction()
    {
		$SESSION = new Container('user');
		if(isset($SESSION['uname']) && $SESSION['uname']!=''){
			$url='http://176.9.51.8:8080/external/server/GetSuiteOrder';
			$data='{"ownerName":"'.$SESSION['uname'].'","ownerType":2}';
			
			$chlead = curl_init();
			curl_setopt($chlead, CURLOPT_URL, $url);
			curl_setopt($chlead, CURLOPT_USERAGENT, 'SugarConnector/1.4');
			curl_setopt($chlead, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));
			curl_setopt($chlead, CURLOPT_VERBOSE, 1);
			curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($chlead, CURLOPT_CUSTOMREQUEST, "POST"); 
			curl_setopt($chlead, CURLOPT_POSTFIELDS,$data);
			curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);
			$dataTotal = curl_exec($chlead);
			$addresserror = curl_error($chlead);
			$dataFinal=json_decode($dataTotal,true);
			$count=count($dataFinal['infoSuiteOrders']);
			if($count!='0'){
				$a=array();
				$b=array();
				$c=array();
				$d=array();
				if(count($dataFinal['infoSuiteOrders'])!='' && count($dataFinal['infoSuiteOrders'])!='0'){
					foreach($dataFinal['infoSuiteOrders'] as $data){ 
						array_push($a,$data['suiteId']);
						array_push($c,$data['availableTime']);
						array_push($d,$data['expireTime']);
					}
				}
				for($i=0;$i<count($a);$i++){
					$url3='http://176.9.51.8:8080/external/server/GetSuite';
					$data3='{"ids":["'.$a[$i].'"]}';
					$chlead3 = curl_init();
					curl_setopt($chlead3, CURLOPT_URL, $url3);
					curl_setopt($chlead3, CURLOPT_USERAGENT, 'SugarConnector/1.4');
					curl_setopt($chlead3, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data3)));
					curl_setopt($chlead3, CURLOPT_VERBOSE, 1);
					curl_setopt($chlead3, CURLOPT_RETURNTRANSFER, true);
					curl_setopt($chlead3, CURLOPT_CUSTOMREQUEST, "POST"); 
					curl_setopt($chlead3, CURLOPT_POSTFIELDS,$data3);
					curl_setopt($chlead3, CURLOPT_SSL_VERIFYPEER, 0);
					$Suitename = curl_exec($chlead3);
					$addresserror3 = curl_error($chlead3);
					$data3=json_decode($Suitename,true);
					array_push($b,$data3['infoSuites'][0]['name']);
				}
				$result= new ViewModel(array(
					"availableTime" 	=>	$c,
					"expireTime" 		=>	$d,
					"Suitename" 		=>	$b,
					"data" 				=>	$dataFinal,
				));
				return $result;
			}
		}else{
			return $this->redirect()->toUrl('login');
		}
	}
}